<template>
  <div>
    <div class="crumbs">
      <el-breadcrumb separator="/">
        <el-breadcrumb-item>
          <i class="el-icon-lx-cascades"></i> 该页面已完成
        </el-breadcrumb-item>
      </el-breadcrumb>
    </div>
    <div class="container">
      <div class="handle-box">
        <el-input
          v-model="query.id"
          placeholder="id"
          class="handle-input mr10"
          clearable
        ></el-input>
        <el-button type="primary" @click="handleSearch">查询</el-button>

        <el-button type="primary" @click="handleAdd">新增</el-button>
        <el-button type="danger" class="handle-del" @click="delAllSelection"
          >删除</el-button
        >
      </div>
      <el-table
        :data="tableData"
        border
        class="table"
        ref="multipleTable"
        header-cell-class-name="table-header"
        @selection-change="handleSelectionChange"
      >
        <el-table-column
          type="selection"
          width="55"
          align="center"
        ></el-table-column>
        <el-table-column
          prop="id"
          label="ID"
          width="55"
          align="center"
        ></el-table-column>
        <el-table-column prop="title" label="标题"></el-table-column>
        <el-table-column prop="content" label="内容"></el-table-column>

        <el-table-column prop="pubDate" label="发布时间"></el-table-column>
        <el-table-column prop="deptId" label="创建者dept_id"></el-table-column>
        <el-table-column prop="creator" label="创建者"></el-table-column>
        <el-table-column prop="createDate" label="创建时间"></el-table-column>
        <el-table-column prop="updater" label="更新者"></el-table-column>
        <el-table-column prop="updateDate" label="更新时间"></el-table-column>

        <el-table-column label="操作" width="180" align="center">
          <template slot-scope="scope">
            <el-button
              type="text"
              icon="el-icon-edit"
              @click="handleEdit(scope.$index, scope.row)"
              >编辑</el-button
            >
            <el-button
              type="text"
              icon="el-icon-delete"
              class="red"
              @click="handleDelete(scope.$index, scope.row)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <div class="pagination">
        <el-pagination
          background
          layout="total,sizes, prev, pager, next, jumper"
          :page-sizes="[5, 10, 20, 30]"
          @size-change="handleSizeChange"
          :current-page="query.pageIndex"
          :page-size="query.pageSize"
          :total="pageTotal"
          @current-change="handlePageChange"
        ></el-pagination>
      </div>
    </div>

    <!-- 编辑弹出框 -->
    <el-dialog title="编辑" :visible.sync="editVisible" width="50%">
      <el-form ref="editForm" :model="editForm" label-width="auto">
        <el-form-item label="用户名">
          <el-input v-model="editForm.name" clearable></el-input>
        </el-form-item>
        <el-form-item label="地址">
          <el-input v-model="editForm.address" clearable></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="editVisible = false">取 消</el-button>
        <el-button type="primary" @click="saveEdit">确 定</el-button>
      </span>
    </el-dialog>

    <!-- 新增弹出框 -->
    <el-dialog title="新增" :visible.sync="addVisible" width="50%">
      <el-form ref="addForm" :model="addForm" label-width="auto" :rules="rules">
        <el-form-item label="标题" prop="title">
          <el-input v-model="addForm.title" clearable></el-input>
        </el-form-item>
        <el-form-item label="内容" prop="content">
          <el-input v-model="addForm.content" clearable></el-input>
        </el-form-item>
        <el-form-item label="发布时间" prop="pubDate">
          <el-input v-model="addForm.pubDate" clearable></el-input>
        </el-form-item>
        <el-form-item label="创建者dept_id" prop="deptId">
          <el-input v-model="addForm.deptId" clearable></el-input>
        </el-form-item>
        <el-form-item label="创建者" prop="creator">
          <el-input v-model="addForm.creator" clearable></el-input>
        </el-form-item>
        <el-form-item label="创建时间" prop="createDate">
          <el-input v-model="addForm.createDate" clearable></el-input>m
        </el-form-item>
        <el-form-item label="更新者" prop="updater">
          <el-input v-model="addForm.updater" clearable></el-input>
        </el-form-item>
        <el-form-item label="更新时间" prop="updateDate">
          <el-input v-model="addForm.updateDate" clearable></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="addVisible = false">取 消</el-button>
        <el-button type="primary" @click="saveAdd">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import util from "utils/util";
export default {
  name: "demonews",
  data() {
    return {
      query: {
        id: "",
        pageIndex: 1,
        pageSize: 20
      },
      tableData: [
        {
          title: "",
          content: "",
          pubDate: "",
          deptId: "",
          creator: "",
          createDate: "",
          updater: "",
          updateDate: ""
        }
      ],
      multipleSelection: [],
      // delList: [],
      editVisible: false,
      addVisible: false,
      pageTotal: 0,
      rules: {
        title: [{ required: true, message: "必填不能为空", trigger: "blur" }],
        content: [{ required: true, message: "必填不能为空", trigger: "blur" }],
        pubDate: [{ required: true, message: "必填不能为空", trigger: "blur" }],
        deptId: [{ required: true, message: "必填不能为空", trigger: "blur" }],
        creator: [{ required: true, message: "必填不能为空", trigger: "blur" }],
        createDate: [
          { required: true, message: "必填不能为空", trigger: "blur" }
        ],
        updater: [{ required: true, message: "必填不能为空", trigger: "blur" }],
        updateDate: [
          { required: true, message: "必填不能为空", trigger: "blur" }
        ]
      },
      editForm: {
        name: "",
        address: ""
      },
      addForm: {
        title: "",
        content: "",
        pubDate: "",
        deptId: "",
        creator: "",
        createDate: "",
        updater: "",
        updateDate: ""
      },
      idx: -1,
      ids: []
    };
  },
  created() {
    this.getDNData({});
  },
  methods: {
    // 获取数据
    getDNData({
      id = "",
      order = "",
      orderField = "",
      page = this.query.pageIndex,
      limit = this.query.pageSize,
      _t = Date.now()
    }) {
      this.$axios
        .get("/demo/news/page", {
          params: {
            id,
            order,
            orderField,
            page,
            limit,
            _t
          }
        })
        .then(({ data: res }) => {
          if (res.code !== 0) {
            return;
          }
          this.pageTotal = res.data.total || 0;
          this.tableData = res.data.list;
        })
        .catch(() => {});
    },
    // 触发搜索按钮
    handleSearch() {
      this.$set(this.query, "pageIndex", 1);
      this.getDNData({ id: this.query.id });
    },
    // 删除操作
    handleDelete(index, row) {
      this.ids[0] = row.id;
      util.delete.call(this, "/demo/news", index, this.ids, this.getDNData);
    },
    saveAdd() {
      // 得到成功提示
      this.$axios
        .post("/demo/news/", {
          title: this.addForm.title,
          content: this.addForm.content,
          pubDate: this.addForm.pubDate,
          deptId: this.addForm.deptId,
          creator: this.addForm.creator,
          createDate: this.addForm.createDate,
          updater: this.addForm.updater,
          updateDate: this.addForm.updateDate
        })
        .then(({ data: res }) => {
          if (res.code !== 0) {
            return;
          }
          this.addVisible = false;
          this.$message.success("操作成功");
          this.getDNData({});
        })
        .catch(() => {});
    },
    // 新增
    handleAdd() {
      this.addVisible = true;
    },
    // 多选操作
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },

    delAllSelection() {
      var ids = this.multipleSelection.map(item => {
        return item.id;
      });
      util.delete.call(this, "/demo/news", "", ids, this.getDNData);
    },
    // 编辑操作
    handleEdit(index, row) {
      this.idx = index;
      this.form = row;
      this.editVisible = true;
    },
    // 保存编辑
    saveEdit() {
      this.editVisible = false;
      this.$message.success(`修改第 ${this.idx + 1} 行成功`);
      this.$set(this.tableData, this.idx, this.form);
    },
    // 分页导航
    handlePageChange(val) {
      this.$set(this.query, "pageIndex", val);
      this.getDNData({});
    },
    handleSizeChange(val) {
      this.$set(this.query, "pageSize", val);
      this.getDNData();
    }
  }
};
</script>

<style scoped>
@import url("../../common/css/index.css");
.mr10 {
  width: 195px;
}
</style>
